package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.io.PrintWriter;
import java.sql.SQLException;
import bean.ConnectMySql;
@WebServlet("/registerUser")
public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      PreparedStatement st=null;
	      Connection con=ConnectMySql.getConnection();
	      request.setCharacterEncoding("UTF-8");
		  try {
				//建立连接 
	            st=con.prepareStatement("insert into login values(null,?,?,?,?)");
	            st.setString(1,request.getParameter("username"));
	            st.setString(2,request.getParameter("password"));
	            st.setString(3,request.getParameter("cellphone"));
	            st.setString(4,request.getParameter("address"));
	            st.executeUpdate();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		  ConnectMySql.closeStatement(st);
		  ConnectMySql.closeConnection(con);
		  response.sendRedirect("login.jsp");
		  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
